var DormSettingForm = function () {
  var template = /*html*/ `
    <div class="dorm-setting-form" v-loading="loading">
      <el-form :model="dorm" ref="dormFormRef" class="dormForm" label-width="70px">
        <el-form-item label="宿舍号码" prop="dormNo">
          <el-input v-model="dorm.dormNo" class="form-input"></el-input>
          <el-button type="primary" @click="addDorm" v-if="type==='update'">添加宿舍</el-button>
        </el-form-item>
        <el-form-item label="电表" prop="eleEnergyId">
          <el-select class="form-input" v-model="dorm.eleEnergyId" clearable="true">
            <el-option v-for="eleEnergy in eleEnergys" v-if="eleEnergy.state===2||eleEnergy.id===dorm.eleEnergyId" 
              :key="eleEnergy.id" :label="eleEnergy.no" :value="eleEnergy.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="水表" prop="waterEnergyId">
          <el-select class="form-input" v-model="dorm.waterEnergyId" clearable="true">
            <el-option v-for="waterEnergy in waterEnergys" v-if="waterEnergy.state===2||waterEnergy.id===dorm.waterEnergyId" 
              :key="waterEnergy.id" :label="waterEnergy.no" :value="waterEnergy.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍人员" prop="userList">
          <el-select multiple filterable class="form-input" v-model="dorm.employeeIds" :reserve-keyword="true" :multiple-limit="10">
            <el-option v-for="employee in employees" v-if="employee.status==='0'||isCanSelect(employee)" :key="employee.id" :label="employee.userName" :value="employee.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电费" prop="defaultElectricFee">
        <el-input-number class="form-input" v-model="dorm.defaultElectricFee" :precision="2" :step="0.01" :max="10" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="水费" prop="defaultWaterFee">
        <el-input-number class="form-input" v-model="dorm.defaultWaterFee" :precision="2" :step="0.01" :max="10" :min="0"></el-input-number>
        </el-form-item>
      </el-form>

      <div v-if="type==='create'" style="text-align:center;margin-bottom:20px;">
        <el-button type="primary" @click="ensure">确定</el-button>
      </div>
      <div v-if="type==='update'&&dorm.id!==''" style="text-align:center;margin-bottom:20px;">
        <el-button type="primary" @click="ensure">修改</el-button>
        <el-button type="danger" @click="deleteDorm">删除</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      dorm: {
        type: Object,
        required: true
      },
      type: {
        type: String,
        default: 'create'
      },
      eleEnergys: {
        type: Array,
        default: []
      },
      waterEnergys: {
        type: Array,
        default: []
      },
      employees: {
        type: Array,
        default: []
      }
    },
    watch: {
      "dorm.id": function () {
        var dorm = this.dorm;
        dorm = _.cloneDeep(dorm);
        this.oldDorm = dorm;
      }
    },
    data: function () {
      var dorm = this.dorm;
      dorm = _.cloneDeep(dorm);
      return {
        loading: false,
        oldDorm: dorm
      };
    },
    methods: {
      addDorm: function () {
        this.$emit('add-drom');
      },

      isCanSelect: function (employee) {
        var type = this.type;
        if (type === 'create') {
          return false;
        }
        var dorm = this.oldDorm;
        var employeeIds = dorm.employeeIds;
        if (employeeIds === undefined) {
          return false;
        }
        return employeeIds.indexOf(employee.id) !== -1;
      },

      ensure: function () {
        var dorm = this.dorm;
        if (!this.checkValidate(dorm)) {
          return;
        }
        var postData = this.getDormPostData(dorm);
        var type = this.type;
        if (type === 'create') {
          this.toAddDorm(postData);
        } else if (type === 'update') {
          this.toUpdateDorm(postData);
        }
      },

      toAddDorm: function (postData) {
        this.loading = true;
        var self = this;
        ajaxRequest('dormManage/addDormManage', {
          data: JSON.stringify(postData)
        }).then(function () {
          self.loading = false;
          self.$alert('添加宿舍成功', '信息');
          self.$emit('add-success');
        }).catch(function () {
          self.loading = false;
        });
      },

      toUpdateDorm: function (postData) {
        var oldDorm = this.oldDorm;
        oldDorm = this.getDormPostData(oldDorm);
        this.loading = true;
        var self = this;
        ajaxRequest('dormManage/updateDormManageByDetail', {
          data: JSON.stringify(postData),
          oldDorm: JSON.stringify(oldDorm)
        }).then(function () {
          self.loading = false;
          self.$alert('修改宿舍成功', '信息');
          self.$emit('update-success');
        }).catch(function () {
          self.loading = false;
        });
      },

      deleteDorm: function () {
        var dorm = this.dorm;
        this.loading = true;
        var self = this;
        ajaxRequest("dormManage/setDormToDisable", {
          dormId: dorm.id
        }).then(function () {
          self.loading = false;
          self.$emit("delete-success");
        }).catch(function () {
          self.loading = false;
        });
      },

      checkValidate: function (dorm) {
        var checkFields = ['dormNo', 'defaultElectricFee', 'defaultWaterFee'];
        if (!CheckVaildUtil.checkNullValue(checkFields, dorm, this.getErrorMsg)) {
          return false;
        }
        return true;
      },

      getErrorMsg: function (code) {
        var errorObj = {
          dormNo: '请输入宿舍号码',
          defaultElectricFee: '请输入电费',
          defaultWaterFee: '请输入水费',
        };
        return errorObj[code];
      },

      getDormPostData: function (dorm) {
        var type = this.type;
        var postData = {
          dormNo: dorm.dormNo
        };
        if (type === 'update') {
          postData.id = dorm.id;
        }

        var eleEnergyId = _.get(dorm, 'eleEnergyId', '');
        _.set(postData, 'energy.id', eleEnergyId);

        var waterEnergyId = _.get(dorm, 'waterEnergyId', '');
        _.set(postData, 'water.id', waterEnergyId);

        var employeeIds = _.get(dorm, 'employeeIds', []);
        if (employeeIds.length > 0) {
          var employees = _.map(employeeIds, function (id) {
            return {
              id: id
            };
          });
          postData.userList = employees;
        }
        postData.defaultElectricFee = dorm.defaultElectricFee;
        postData.defaultWaterFee = dorm.defaultWaterFee;
        return postData;
      }
    },
    template: template
  };
}();